﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Forest.Cat.Domain.Entities.Resumes;
using Forest.Core;
using Forest.Core.Data;
using Forest.Core.Caching;
using Forest.Core.Events;
using Forest.Cat.Domain.Shared;

namespace Forest.Cat.Domain.Services.Resumes
{
    public class EducationService : IEducationService
    {
        #region Constants
        private const string Education_BY_ID_KEY = "education.id-{0}";
        private const string Education_PATTERN_KEY = "education.";
        #endregion

        #region Fields
        private readonly IRepository<Education> _educationRepository;
        private readonly ICacheManager _cacheManager;
        private readonly IEventPublisher _eventPublisher;
        private readonly IGenerateNumberService _generateNumberService;
        #endregion

        #region Ctor
        public EducationService(IRepository<Education> workExperienceRepository,
            ICacheManager cacheManager, IEventPublisher eventPublisher, IGenerateNumberService generateNumberService)
        {
            _educationRepository = workExperienceRepository;
            _cacheManager = cacheManager;
            _eventPublisher = eventPublisher;
            _generateNumberService = generateNumberService;
        }
        #endregion

        #region Base Methods
        public virtual void DeleteEducation(Education education)
        {
            if (education == null)
                throw new ArgumentNullException("Education");

            _educationRepository.Delete(education);

            _cacheManager.RemoveByPattern(Education_PATTERN_KEY);
            //event notification
            _eventPublisher.EntityDeleted(education);

        }

        public Education GetEducationById(Guid educationId)
        {
            if (educationId == Guid.Empty)
                return null;

            string key = string.Format(Education_BY_ID_KEY, educationId);

            return _cacheManager.Get(key, () =>
            {
                var cl = _educationRepository.GetById(educationId);
                return cl;
            });
        }

        public virtual IPagedList<Education> GetAllEducations(int pageIndex, int pageSize)
        {
            var query = _educationRepository.Table;
            query = query.OrderBy(w => w.Id);
            var items = new PagedList<Education>(query, pageIndex, pageSize);
            return items;
        }

        public virtual IList<Education> GetEducations(Guid resumeId)
        {
            var query = _educationRepository.Table;
            query = query.Where(c => c.ResumeId == resumeId).OrderByDescending(c => c.LastUpdated);

            return query.ToList();
        }

        public virtual void InsertEducation(Education education)
        {
            if (education == null)
                throw new ArgumentNullException("Education");

            _educationRepository.Insert(education);

            _cacheManager.RemoveByPattern(Education_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityInserted(education);
        }

        public virtual void UpdateEducation(Education education)
        {
            if (education == null)
                throw new ArgumentNullException("Education");
            _educationRepository.Update(education);

            _cacheManager.RemoveByPattern(Education_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityUpdated(education);
        }
        #endregion

        public Guid CreateId
        {
            get
            {
                return _generateNumberService.GenerateId<Education>(_educationRepository, GenerateType.Education);
            }
        }

        public bool HasEducations(Guid resumeId)
        {
            return _educationRepository.Table.Any(s => s.ResumeId == resumeId);
        }
    }
}
